<?php if(navigation::getAdminAction() === 'create' || navigation::getAdminAction() === 'update' ): ?>
<?php

if(navigation::getAdminModeleId()>0){
    $category = new modele_category(navigation::getAdminModeleId());
    $data = $category->getDatas();
    $data['permalien'] = $category->getPermalien()->getCleanedLien(false);
    $action = 'update';
}else{
    $action = 'create';
    $data = array();
}
?><div class="columns thirteen">
    <h1><a class="pwe-ui-btn" href="?template=category"><span class="ui-icon ui-icon ui-icon-arrowthick-1-w"></span></a> Modifier/créer une rubrique</h1>
    <hr style="margin: 5px 0 15px 0;">
</div>
<form method="post" enctype="multipart/form-data" action="?template=category&action=<?php echo $action; ?>&modele=category&do=1&modele_id=<?php echo isset($data['id'])?$data['id']:0; ?>">
    <div class="columns nine">
        <div align="right">
            <input type="submit" value="Enregistrer" class="pwe-ui-btn"/> 
            <?php if(isset($data['id']) && $data['id']>0): ?><a href="<?php echo $data['url']; ?>" target="_blank" title="voir" class="pwe-ui-btn">voir</a><?php endif; ?>
        </div>
        <?php echo $this->getInputHtml('Titre', 'modele_title', (isset($data['title']))?$data['title']:'', 'category', (isset($data['id']))?$data['id']:0, array('class_input'=>'titreModele','actions'=>array('onblur'=>"javascript:generatePermalien($(this).val());"))); ?>
        <?php echo $this->getInputHtml('Permalien', 'modele_lien', (isset($data['permalien']))?$data['permalien']:'', 'category', (isset($data['id']))?$data['id']:0, array('prefix'=>PWE::getBaseURL(),'sufix'=>modele_permalien::getExtensionByModele('category'),'id_input'=>'permalien','actions'=>array('onblur'=>"javascript:validPermalien($(this).val());"))); ?>
        
        <?php include('inc/seoManager.inc.php'); ?>
        <?php include('inc/redirectionManager.inc.php'); ?>
        <div align="right">
            <input type="submit" value="Enregistrer" class="pwe-ui-btn"/> 
            <?php if(isset($data['id']) && $data['id']>0): ?><a href="<?php echo $data['url']; ?>" target="_blank" title="voir" class="pwe-ui-btn">voir</a><?php endif; ?>
        </div>
    </div>
    <div class="columns four">
        <?php include('inc/statutManager.inc.php'); ?>
        <br/><br/>
        <?php include('inc/templateManager.inc.php'); ?>
        <br/><br/>
        <h3>Est une sous-rubrique de :</h3>
        <select name="modele_parent_id">
            <option value="0">racine du site</option>
            <?php foreach(modele_category::getPrimaryListe() as $category): ?>
            <?php
                if(isset($data['parent_id'])&&$data['parent_id'] == $category->getId()){
                    $selected = ' selected="selected"';
                }else{
                    $selected = '';
                }
            ?>
            <?php if(isset($data['id'])&&$category->getId()===$data['id']): ?>
            <option class="desable"><?php echo $category->getTitle(); ?></option>
            <?php else: ?>
            <option value="<?php echo $category->getId(); ?>"<?php echo $selected; ?>><?php echo $category->getTitle(); ?></option>
            <?php endif; ?>    
                <?php if($category->haveChild()): ?>
                <?php foreach($category->getChild() as $sCategory): ?>
                <?php
                    if(isset($data['parent_id'])&&$data['parent_id'] == $sCategory->getId()){
                        $selected = ' selected="selected"';
                    }else{
                        $selected = '';
                    }
                ?>
                <?php if(isset($data['id'])&&$sCategory->getId()===$data['id']): ?>
                <option class="desable"> - <?php echo $sCategory->getTitle(); ?></option>
                <?php else: ?>
                <option value="<?php echo $sCategory->getId(); ?>"<?php echo $selected; ?>> - <?php echo $sCategory->getTitle(); ?></option>
                <?php endif; ?>
                
                    <?php if($sCategory->haveChild()): ?>
                    <?php foreach($sCategory->getChild() as $ssCategory): ?>
                    <?php
                        if(isset($data['parent_id'])&&$data['parent_id'] == $ssCategory->getId()){
                            $selected = ' selected="selected"';
                        }else{
                            $selected = '';
                        }
                    ?>
                    <?php if(isset($data['id'])&&$ssCategory->getId()===$data['id']): ?>
                    <option class="desable"> - - <?php echo $ssCategory->getTitle(); ?></option>
                    <?php else: ?>
                    <option value="<?php echo $ssCategory->getId(); ?>"<?php echo $selected; ?>> - - <?php echo $ssCategory->getTitle(); ?></option>
                    <?php endif; ?>
                    <?php endforeach; ?>
                    <?php endif; ?>
                    
                <?php endforeach; ?>
                <?php endif; ?>
            
            <?php endforeach;?>
        </select><br/><br/>
        <?php include('inc/mediaManager.inc.php'); ?>
        <input type="hidden" name="modele_id" value="<?php echo (isset($data['id']))?$data['id']:0; ?>" />
    </div>
</form>
<?php else: ?>
<div class="columns thirteen">
<h1>Liste des rubriques</h1>
<hr style="margin: 5px 0 25px 0;">
    <div align="right">
        <a class="pwe-ui-btn" href="?template=category&modele=category&action=create">Créer une rubrique</a>
    </div><br/>
    <table class="liste ui-widget" width="100%">
        <thead class="ui-widget-header">
            <tr>
                <th>Nom</th>
                <th>Permalien</th>
                <th>Articles</th>
                <th>Modèle</th>
                <th>Parent</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody class="ui-widget-content">
            <?php foreach(modele_category::getPrimaryListe() as $category): ?>
            <?php $data = $category->getDatas(); ?>
            <tr>
                <td>
                    <?php echo $data['title']; ?><br/>
                    <b><?php echo $data['statut']; ?></b>
                </td>
                <td><?php echo $data['permalien'] ?></td>
                <td style="text-align: center;">
                    <?php echo $category->getNombrePublications(); ?>
                    <a class="pwe-ui-btn" href="?template=publication&modele=publication&category_id=<?php echo $data['id']; ?>" title="Voir la liste"><span class="ui-icon ui-icon-folder-open"></span></a>
                </td>
                <td><?php echo $data['template']; ?></td>
                <td><?php echo ($category->haveParent())?$category->getParent()->getTitle():'-'; ?></td>
                <td>
                    <a class="pwe-ui-btn" href="<?php echo $data['url']; ?>" target="preview_site" title="Voir"><span class="ui-icon ui-icon-extlink"></span></a>
                    <a class="pwe-ui-btn" href="?template=category&modele=category&action=update&modele_id=<?php echo $data['id']; ?>" title="Modifier"><span class="ui-icon ui-icon-pencil"></span></a>
                    <a class="pwe-ui-btn" href="?template=publication&modele=publication&action=create&category_id=<?php echo $data['id']; ?>" title="Ajouter un article"><span class="ui-icon ui-icon-plus"></span></a>
                    <a href="?template=category&modele=category&action=delete&do=1&modele_id=<?php echo $data['id']; ?>" class="confirmAction pwe-ui-btn" title="Supprimer la rubrique '<?php echo $data['title']; ?>' ainsi que toutes les sous-rubriques et articles associés"><span class="ui-icon ui-icon-trash"></span></a>
                </td>
            </tr>
                <?php if($category->haveChild()): ?>
                <?php foreach($category->getChild() as $sCategory): ?>
                <?php $sData = $sCategory->getDatas(); ?>
                <tr class="sous-rubrique">
                    <td>
                        <span>- <?php echo $sData['title']; ?></span>
                    </td>
                    <td><?php echo $sData['permalien'] ?></td>
                    <td style="text-align: center;">
                        <?php echo $sCategory->getNombrePublications(); ?>
                        <a class="pwe-ui-btn" href="?template=publication&modele=publication&category_id=<?php echo $sCategory->getId(); ?>" title="Voir la liste"><span class="ui-icon ui-icon-folder-open"></span></a>
                    </td>
                    <td><?php echo $sData['template']; ?></td>
                    <td><?php echo ($sCategory->haveParent())?$sCategory->getParent()->getTitle():'-'; ?></td>
                    <td>
                        <a class="pwe-ui-btn" href="<?php echo $sData['url']; ?>" target="_blank" title="Voir"><span class="ui-icon ui-icon-extlink"></span></a>
                        <a class="pwe-ui-btn" href="?template=category&modele=category&action=update&modele_id=<?php echo $sData['id']; ?>" title="Modifier"><span class="ui-icon ui-icon-pencil"></span></a>
                        <a class="pwe-ui-btn" href="?template=publication&modele=publication&action=create&category_id=<?php echo $sData['id']; ?>" title="Ajouter un article"><span class="ui-icon ui-icon-plus"></span></a>
                        <a href="?template=category&modele=category&action=delete&do=1&modele_id=<?php echo $sData['id']; ?>" class="confirmAction pwe-ui-btn" title="Supprimer la rubrique '<?php echo $sData['title']; ?>' ainsi que toutes les sous-rubriques et articles associés"><span class="ui-icon ui-icon-trash"></span></a>
                    </td>
                </tr>                
                    <?php if($sCategory->haveChild()): ?>
                    <?php foreach($sCategory->getChild() as $ssCategory): ?>
                    <?php $ssData = $ssCategory->getDatas(); ?>
                    <tr class="sous-sous-rubrique">
                        <td>
                            <span>-- <?php echo $ssData['title']; ?></span>
                        </td>
                        <td><?php echo $ssData['permalien'] ?></td>
                        <td style="text-align: center;">
                            <?php echo $ssCategory->getNombrePublications(); ?>
                            <a class="pwe-ui-btn" href="?template=publication&modele=publication&category_id=<?php echo $ssCategory->getId(); ?>" title="Voir la liste"><span class="ui-icon ui-icon-folder-open"></span></a>
                            
                        </td>
                        <td><?php echo $ssData['template']; ?></td>
                        <td><?php echo ($ssCategory->haveParent())?$ssCategory->getParent()->getTitle():'-'; ?></td>
                        <td>
                            <a class="pwe-ui-btn" href="<?php echo $ssData['url']; ?>" target="_blank" title="Voir"><span class="ui-icon ui-icon-extlink"></span></a>
                            <a class="pwe-ui-btn" href="?template=category&modele=category&action=update&modele_id=<?php echo $ssData['id']; ?>" title="Modifier"><span class="ui-icon ui-icon-pencil"></span></a>
                            <a class="pwe-ui-btn" href="?template=publication&modele=publication&action=create&category_id=<?php echo $ssData['id']; ?>" title="Ajouter un aticle"><span class="ui-icon ui-icon-plus"></span></a>
                            <a href="?template=category&modele=category&action=delete&do=1&modele_id=<?php echo $ssData['id']; ?>" class="confirmAction pwe-ui-btn" title="Supprimer la rubrique '<?php echo $ssData['title']; ?>' ainsi que toutes les sous-rubriques et articles associés"><span class="ui-icon ui-icon-trash"></span></a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php endif; ?>
                <?php endforeach; ?>
                <?php endif; ?>
            <?php endforeach; ?>
        </tbody>
        <tfoot></tfoot>
    </table>
</div>
<?php endif; ?>
